package com.huluobo.a2107mvpimdemo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huluobo.a2107mvpimdemo.R;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.util.DateUtils;

import org.w3c.dom.Text;

import java.util.Date;

/**
 * 接收消息的自定义view
 * Created by LC on 2023/10/8.
 */
public class ReceiveMessageView extends RelativeLayout {
    public ReceiveMessageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.view_receive_message_item, this);
    }

    public void bindView(EMMessage emMessage, boolean showTimestamp) {
        updateMessage(emMessage);
        updateTimestamp(emMessage, showTimestamp);
    }

    private void updateTimestamp(EMMessage emMessage, boolean showTimestamp) {
        TextView receive_message_timestamp = findViewById(R.id.receive_message_timestamp);
        if (showTimestamp) {
            receive_message_timestamp.setVisibility(View.VISIBLE);
            receive_message_timestamp.setText(DateUtils.getTimestampString(new Date(emMessage.getMsgTime())));
        } else {
            receive_message_timestamp.setVisibility(View.GONE);
        }
    }

    private void updateMessage(EMMessage emMessage) {
        TextView receive_message_tv = findViewById(R.id.receive_message_tv);
        if (emMessage.getType() == EMMessage.Type.TXT) {
            receive_message_tv.setText(((EMTextMessageBody) emMessage.getBody()).getMessage());
        } else {
            receive_message_tv.setText("非文本消息");
        }
    }
}
